import { request } from '@inbiz/utils';
const version = InbizConfig.API_VERSION;

export type ApiTypeByPage = {
  Items: {
    Remark: string;
    MethodName: string;
    MethodKey: string;
    ServerKey: string;
    OutParm: string;
  }[];
  TotalCount: number;
};

export type InputType = {
  BodyParms: {
    Description: string;
    Name: string;
    Required: boolean;
    Type: number;
    Value: any;
  }[];
  HeaderParms: {
    Description: string;
    Name: string;
    Required: boolean;
    Type: number;
    Value: any;
  }[];
  PathParms: {
    Description: string;
    Name: string;
    Required: boolean;
    Type: number;
    Value: any;
  }[];
  QueryParms: {
    Description: string;
    Name: string;
    Required: boolean;
    Type: number;
    Value: any;
  }[];
};

//获取入参
export async function getApi(params: {
  appId: string;
  MethodKey: string;
}): Promise<InputType> {
  return await request(
    `/inbiz/api/services/front/interfaceengine/v${version}/interfaceMethod/inputParm`,
    {
      params,
    },
  );
}

export type SeverType = {
  Id: string;
  ServerKey: string;
  ServerName: string;
}[];

//查询连接器
export async function getServer(params: { appId: string }): Promise<SeverType> {
  return await request(
    `/inbiz/api/services/front/interfaceengine/v${version}/interfaceServer/server`,
    {
      params,
    },
  );
}

//通过serverKey分页查询接口
export async function getMethod(params: {
  appId: string;
  serverKey: string;
  skipCount: number;
  maxResultCount: number;
  contain?: string;
  groupKey?: string;
  isContaisInnerGroup?: boolean;
}): Promise<ApiTypeByPage> {
  return await request(
    `/inbiz/api/services/front/interfaceengine/v${version}/interfaceMethod/methodPaged`,
    {
      params,
    },
  );
}

type inputType = {
  name: string; //参数名称
  value: string; //参数值
  describe: string; //描述
  controlVar: string; //控件变量
  type: 0 | 1 | 2 | 3; //类型
};

type outputType = {
  name: string; //参数名称
  path: string; //路径
  successMark: string; //成功标识
  type: 0 | 1 | 2; //类型
  isSingle: boolean; //唯一标识
  childrens?: {
    name: string; //参数名称
    path: string; //路径
    successMark: string; //成功标识
    type: 0 | 1 | 2; //类型
    isSingle: boolean; //唯一标识
  };
};

type QueryConfig = {
  selectConfig: {
    //MethodKey
    key: string;
    //入参参数
    inputConfig: inputType[];
    //出参参数
    outConfig: outputType;
  };
  editConfig?: {
    key: string;
    inputConfig: inputType[];
    outConfig: outputType;
  };
  deleteConfig?: {
    key: string;
    inputConfig: inputType[];
    outConfig: outputType;
  };
};

//接口工厂存储
export async function setApi(data: {
  appId: string;
  name: string;
  groupkey: string;
  souceType: number;
  interfaceQueryConfig: QueryConfig;
}): Promise<any> {
  return await request.post(
    `/inbiz/api/services/modelengine/v${version}/modelQuery`,
    {
      data,
    },
  );
}

//接口工厂更新
export async function updateApi(data: {
  appId: string;
  name: string;
  souceType: number;
  groupkey: string;
  interfaceQueryConfig: QueryConfig;
  key: string;
}): Promise<any> {
  return await request.put(
    `/inbiz/api/services/modelengine/v${version}/modelQuery`,
    {
      data,
    },
  );
}

//接口工厂查询
export async function getApiData(params: {
  appId: string;
  key: string;
  pageInfo: {
    index: number;
    size: number;
  };
}): Promise<any> {
  return await request(
    `/inbiz/api/services/modelengine/v${version}/modelQuery/queryInfo`,
    {
      params,
    },
  );
}

//通过methodKey查询接口数据
export async function getMethodKeyData(params: {
  appId: string;
  methodKey: string;
}): Promise<any> {
  return await request(
    `/inbiz/api/services/front/interfaceengine/v${version}/interfaceMethod/methodByMethodKey`,
    {
      params,
    },
  );
}

export type TreeListData = {
  Name: string;
  Id: string;
  Children: TreeListData;
}[];
export async function getTreeList({
  appId,
  ...other
}: {
  appId: string;
  lowestLevel: string;
}): Promise<TreeListData> {
  return await request(
    `/inbiz/api/services/front/interfaceengine/v${version}/interfaceServer/treeList/${appId}`,
    {
      params: other,
    },
  ).then((res) => {
    if (res.length) {
      res.forEach((item: { Children: any[]; ServerKey: any; Id: any }) => {
        if (item?.Children?.length) {
          item.Children.forEach((child) => {
            if (child.Id === '00000000000000000000000000000000') {
              child.Id = item.Id + '_' + child.Id;
            }
            child.ServerKey = item.Id;
            if (child?.Children?.length) {
              child.Children.forEach((sun: { ServerKey: any }) => {
                sun.ServerKey = item.Id;
              });
            }
          });
        }
      });
    }
    return res;
  });
}
